package com.czmh.etc.onlinepubsys.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.czmh.etc.common.core.annotation.ApiVersion;
import com.czmh.etc.onlinepubsys.dto.UpdateUserInfo;
import com.czmh.etc.onlinepubsys.feign.newrun.entity.EtcUser;
import com.czmh.etc.onlinepubsys.json.JsonResult;
import com.czmh.etc.onlinepubsys.model.Etcusercarinfo;
import com.czmh.etc.onlinepubsys.services.EtcUserInfoSevice;
import com.czmh.etc.onlinepubsys.services.IEtcusercarinfoService;
import com.czmh.etc.onlinepubsys.utils.JsonResultUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @ClassName：EtcUserController
 * @Description：用户中心接口
 * @Author： sevenchen. 
 * @Date：2019/10/14 10:35. 
 * @Version：1.0.0
 */
@RestController
@RequestMapping("/{version}/api/etcUser/")
@Api(tags = "用户中心接口")
public class EtcUserController {

    @Autowired
    private EtcUserInfoSevice      etcUserInfoSevice;
    @Autowired
    private IEtcusercarinfoService iEtcusercarinfoService;

    @ApiVersion(1)
    @PostMapping(value = "bindMobile")
    @ApiOperation(value = "微信授权首次登录时绑定手机号", notes = "微信授权首次登录时绑定手机号")
    public JsonResult bindMobile(@RequestBody EtcUser etcUser) throws Exception {
        return etcUserInfoSevice.bindMobile(etcUser);
    }

    @ApiVersion(1)
    @PostMapping(value = "updateUserInfo")
    @ApiOperation(value = "更新用户信息", notes = "更新用户信息")
    public JsonResult updateUserInfo(@RequestBody UpdateUserInfo userInfo) throws Exception {
        return etcUserInfoSevice.updateUserInfo(userInfo);
    }

    @ApiVersion(1)
    @PostMapping(value = "updateUserCar")
    @ApiOperation(value = "添加/修改我的车辆", notes = "添加/修改我的车辆")
    public JsonResult updateUserCar(@RequestBody @Validated Etcusercarinfo etcusercarinfo) throws Exception {
        iEtcusercarinfoService.saveOrUpdate(etcusercarinfo);
        String carId = etcusercarinfo.getCarId();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("carId", carId);
        return JsonResultUtil.toResult(map);
    }
    
    @ApiVersion(1)
    @PostMapping(value = "{userid}")
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    public JsonResult users(@PathVariable String userid) throws Exception {
        return etcUserInfoSevice.users(userid);
    }
    
    @ApiVersion(1)
    @PostMapping(value = "mycar/{userid}")
    @ApiOperation(value = "获取用户添加的车辆信息", notes = "获取用户添加的车辆信息")
    public JsonResult mycar(@PathVariable String userid) throws Exception {
        QueryWrapper<Etcusercarinfo> queryWrapper = new QueryWrapper<Etcusercarinfo>();
        queryWrapper.lambda().eq(Etcusercarinfo::getUserId, userid);
        List<Etcusercarinfo> cars = iEtcusercarinfoService.list(queryWrapper);
        return JsonResultUtil.toResult(cars);
    }
    
    @ApiVersion(1)
    @PostMapping(value = "updateMobile")
    @ApiOperation(value = "更改绑定的手机号", notes = "更改绑定的手机号")
    public JsonResult updateMobile(@ApiParam(value = "用户id") @RequestParam String userId,
                                    @ApiParam(value = "手机号") @RequestParam String mobile,
                                    @ApiParam("验证码") @RequestParam String validCode) throws Exception {
        return etcUserInfoSevice.updateMobile(userId, mobile, validCode);
    }
    
    
}
